package managedbean.PurchaseOrder;

import entity.LineItem;
import entity.SaleQuotation;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.PurchaseOrderBeanLocal;
import session.stateless.SaleQuotationBeanLocal;

@ManagedBean(name="addSOWithSQ")
@ViewScoped

public class AddPurchaseOrderWithQuotationManagerBean implements Serializable {
    
    public AddPurchaseOrderWithQuotationManagerBean () {
    }
    
    @EJB
    private PurchaseOrderBeanLocal purchaseOrderBeanLocal;
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    
    private SaleQuotation selectedSaleQuotation;
    private Long purchaseOrderId;
    private List<LineItem> lineItems;

    private String statusMessage;

    public void initView(PhaseEvent event)
    {        
        if (selectedSaleQuotation == null)                
        {
            selectedSaleQuotation = (SaleQuotation)FacesContext.getCurrentInstance().getExternalContext().getFlash().get("selectedSaleQuotation");            
        }
        
        try
        {
            List<LineItem> temp_lineItems = new ArrayList<LineItem>();
                for (LineItem temp:selectedSaleQuotation.getLineItems())
                {
                    temp_lineItems.add(temp);                                        
                }
            this.setLineItems(temp_lineItems);     
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public PurchaseOrderBeanLocal getPurchaseOrderBeanLocal() {
        return purchaseOrderBeanLocal;
    }

    public void setPurchaseOrderBeanLocal(PurchaseOrderBeanLocal purchaseOrderBeanLocal) {
        this.purchaseOrderBeanLocal = purchaseOrderBeanLocal;
    }

    public String getStatusMessage() {
        return statusMessage;
    }

    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public SaleQuotation getSelectedSaleQuotation() {
        return selectedSaleQuotation;
    }

    public void setSelectedSaleQuotation(SaleQuotation selectedSaleQuotation) {
        this.selectedSaleQuotation = selectedSaleQuotation;
    }

    public Long getPurchaseOrderId() {
        return purchaseOrderId;
    }

    public void setPurchaseOrderId(Long purchaseOrderId) {
        this.purchaseOrderId = purchaseOrderId;
    }

    public void saveNewPurchaseOrder(ActionEvent event)
    {
        List<LineItem> temp_lineItems = lineItems;
               
        try
        {                          
            purchaseOrderId = purchaseOrderBeanLocal.convertToPurchaseOrder(selectedSaleQuotation.getSaleLead().getRegistrationNumber(), selectedSaleQuotation.getId(), temp_lineItems);
            statusMessage = "New Sale Order Saved Successfully.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Sale Order Result: " + statusMessage + " (New Sale Order ID: " + purchaseOrderId + ")", ""));
        }
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
        
    }
}
